package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.domain.FieldGroup;
import com.foreverwin.jzymes.common.mapper.FieldGroupAdviceMapper;
import com.foreverwin.jzymes.common.mapper.FieldGroupMapper;
import com.foreverwin.jzymes.common.service.IFieldGroupAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class FieldGroupAdviceServiceImpl implements IFieldGroupAdviceService {

    @Autowired
    private FieldGroupMapper fieldGroupMapper;
    @Autowired
    private FieldGroupAdviceMapper fieldGroupAdviceMapper;

    /**
     * 获取主数据字段组
     *
     * @param fieldGroupBo 主数据字段BO
     * @return
     */
    public String getFieldGroup( String fieldGroupBo ){
        Assert.hasText( fieldGroupBo, I18nUtil.getI18nText( "common.fieldGroupNotBlank" ) );
        FieldGroup fieldGroup = fieldGroupMapper.selectFieldGroupById( fieldGroupBo );
        Assert.notNull( fieldGroup, I18nUtil.getI18nText( "common.fieldGroupNotExist", new String[]{ fieldGroupBo } ) );
        return fieldGroup.getFieldGroup();
    }

    /**
     * 获取主数据字段值
     *
     * @param fieldGroupBo 主数据字段BO
     * @return
     */
    public String getFieldName( String fieldGroupBo ){
        Assert.hasText( fieldGroupBo, I18nUtil.getI18nText( "common.fieldGroupNotBlank" ) );
        FieldGroup fieldGroup = fieldGroupMapper.selectFieldGroupById( fieldGroupBo );
        Assert.notNull( fieldGroup, I18nUtil.getI18nText( "common.fieldGroupNotExist", new String[]{ fieldGroupBo } ) );
        return fieldGroup.getFieldName();
    }

    /**
     * 主数据字段是否存在
     *
     * @param site
     * @param fieldGroup
     * @param fieldName
     * @return
     */
    public FieldGroup existFieldGroup( String site, String fieldGroup, String fieldName ){
        String handle = BOHandleUtil.buildFieldGroupHandle( site, fieldName );
        FieldGroup _fieldGroup = fieldGroupMapper.selectFieldGroupById( handle );
        Assert.notNull( _fieldGroup, I18nUtil.getI18nText( "common.fieldGroupNotExist", new String[]{ handle } ) );
        return _fieldGroup;
    }

    /**
     * 检查主数据字段是否存在
     *
     * @param fieldGroupBo
     * @return
     */
    public FieldGroup existFieldGroup( String fieldGroupBo ){
        Assert.hasText( fieldGroupBo, I18nUtil.getI18nText( "common.fieldGroupBoNotBlank" ));
        FieldGroup _fieldGroup = fieldGroupMapper.selectFieldGroupById( fieldGroupBo );
        Assert.notNull( _fieldGroup, I18nUtil.getI18nText( "common.fieldGroupNotExist", new String[]{ fieldGroupBo } ) );
        return _fieldGroup;
    }

    /**
     * 查询字段列表
     *
     * @param site
     * @param fieldGroup
     * @return
     */
    public List<FieldGroup> findFieldListByGroup( String site, String fieldGroup ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
        Assert.hasText( fieldGroup, I18nUtil.getI18nText( "common.fieldGroupBoNotBlank" ));
        List<FieldGroup> fieldList = fieldGroupAdviceMapper.selectFieldListByGroup( site, fieldGroup );
        return fieldList;
    }
}
